package pri.zxx.learndemo.threademo.base;

import lombok.extern.slf4j.Slf4j;
import pri.zxx.webdemo.utils.TimeUtils;

import static java.lang.Thread.currentThread;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-06-26-23:29
 */
@Slf4j
public class StopThreadDemo {
    public static void main(String[] args) {

        Runnable target = () -> {
            while (!currentThread().isInterrupted()) {
                log.warn(currentThread().getName() + "##hello");
                //线程中使用了可中断方法后， isInterrupted()将被复位 不能通过该标识 来结束线程
                //可通过捕获 InterruptedException 来结束线程
                TimeUtils.pause(0, 0, 500);
            }
        };
        Thread ts = new Thread(target, "stop-test-");
        ts.start();
        TimeUtils.pause(0, 2, 0);
        ts.interrupt();

        TimeUtils.pause(0, 2, 0);

    }

}
